//
//  BaseSectionTableView.m
//  智云网校
//
//  Created by 毕武侠 on 2017/6/1.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "BaseSectionTableView.h"
#import "BaseTableViewHeaderFooterView.h"

@interface BaseSectionTableView () <HeaderFooterDelegate, cellActionDelegate>

@property (nonatomic, strong) NSString *headClassName;
@property (nonatomic, strong) NSString *headIdentifier;

@property (nonatomic, strong) NSString *footClassName;
@property (nonatomic, strong) NSString *footIdentifier;

@end

@implementation BaseSectionTableView

- (void)registerHeadOrFootView:(NSString *)view class:(NSString *)className reuseIdentifier:(NSString *)identifier {
    if ([view isEqualToString:@"head"]) {
        self.headClassName = className;
        self.headIdentifier = identifier;
    } else if ([view isEqualToString:@"foot"]) {
        self.footClassName = className;
        self.footIdentifier = identifier;
    }
    [self registerClass:NSClassFromString(className) forHeaderFooterViewReuseIdentifier:identifier];
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    BaseSectionModel *model = self.sourceData[section];
    return model.headHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    BaseSectionModel *model = self.sourceData[section];
    return model.footHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BaseSectionModel *model = self.sourceData[section];
    if (model.headHeight == 0) {
        return nil;
    }
    BaseTableViewHeaderFooterView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headIdentifier];
    headView.section = section;
    headView.delegate = self;
    [headView bindModel:model];
    return headView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    BaseSectionModel *model = self.sourceData[section];
    if (model.footHeight == 0) {
        return nil;
    }
    
    BaseTableViewHeaderFooterView *footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footIdentifier];
    footView.section = section;
    footView.delegate = self;
    [footView bindModel:model];
    return footView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sourceData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BaseSectionModel *model = self.sourceData[section];
    return model.child.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseSectionModel *model = self.sourceData[indexPath.section];
    BaseModel *cModel = model.child[indexPath.row];
    return cModel.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
    if (!cell) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.identifier];
    }
    
    BaseSectionModel *model = self.sourceData[indexPath.section];
    BaseModel *cModel = model.child[indexPath.row];
    
    cell.indexPath = indexPath;
    cell.actionDelegate = self;
    cell.data = self.data;
    [cell bindModel:cModel];
    return cell;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
